<?php
namespace jy_cxjxiche;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class order extends \plugin{
	//订单创建时，执行此方法，执行相关操作
	public function create($params,$uid='',$url=''){
		$this->order=$params;
		$this->order['createtime']=TIMESTAMP;
		$uid=$uid?$uid:$_SESSION['uid'];
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'ordersn'=>$params['ordersn']?:get_ordersn('jy_cxjxiche_order','ordersn'),
			'mid'=>$params['mid']?:$uid,
			'address'=>$params['address'],
			'goods'=>ijson_encode($params['goods']),
			'gocity'=>$params['gocity'],
			'price'=>$params['totalprice'],
			'time'=>$params['time'],
			'total'=>$params['total'],
			'type'=>$params['type'],
			'day'=>$params['day'],
			'name'=>$params['name'],
			'mobile'=>$params['mobile'],
			'store_id'=>$params['store_id'],
			'worker_id'=>$params['worker_id'],
		);
		$order['createtime']=TIMESTAMP;
		pdo_insert('jy_cxjxiche_order',$order);
		$orderid=pdo_insertid();
		$params=array(
			'mid'=>$order['mid'],
			'message'=>'您呼叫了一次洗车！',
		);
		$this->model('notice')->add_msg($params);
		return $orderid;
	}
	public function get_order($order_id,$merchid=0){
		if($this->order && $this->order['id']==$order_id){
			return $this->order;
		}
		$this->order=$this->get('order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		return $this->order;
	}
	public function get_fee($goods=array()){
		$totalprice=0;
		$total=0;
		$newgoods=array();
		foreach($goods as $k=>&$item){
			if($item!=1){
				continue;
			}
			$serve=pdo_get('jy_cxjxiche_serve',array('id'=>$k,'uniacid'=>$_SESSION['uniacid']),array('id','title','price','image'));	
			$serve['image']=tomedia($serve['image']);
			$totalprice +=$serve['price'];
			$total++;
			$newgoods[]=$serve;
		}
		$fees['totalprice']=$totalprice;
		$fees['total']=$total;
		$fees['goods']=$newgoods;
		return $fees;
	}
	//订单取消
	public function order_cancel($order_id,$istask=false){
		$this->get_order($order_id);
		if(!$this->order || ($this->order['status'] !=0 && $this->order['status'] !=2 && $this->order['status'] !=6)){
			if($istask){
				return false;
			}else{
				exi('订单不存在或者不支持取消！','error');
			}
		}
		$this->update('order',array('status'=>5),array('id'=>$order_id));
		$params=array(
			'mid'=>$this->order['mid'],
			'message'=>'您的《'.$this->order['ordersn'].'》订单已取消！',
		);
		$this->model('notice')->add_msg($params);
		if($this->order['worker_id']){
			pdo_update('jy_cxjxiche_worker',array('busy'=>1),array('id'=>$this->order['worker_id']));
		}
		return true;
	}
	//支付
	public function order_pay($order_id,$params,$url=''){
		$this->get_order($order_id);
		if(!$this->order || $this->order['ispay']){
			exi('订单不存在或者已支付！','error');
		}
		$update=array(
				'status'=>1,
				'paytype'=>$params['paytype'],
				'transid'=>$params['transid'],
		);
		if($params['paytype']=='hdfk'){
			$update['ispay']=0;
		}else{
			$update['ispay']=1;
			$update['pay_time']=TIMESTAMP;
		}
		$params=array(
			'mid'=>$this->order['mid'],
			'message'=>'您的《'.$this->order['ordersn'].'》订单已支付！',
		);
		$this->model('notice')->add_msg($params);
		$this->pdo_update('order',$update,array('id'=>$order_id));
		return true;
	}
	public function add_baobei($order_id,$msg='',$oldstatus=2,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'oldstatus'=>$oldstatus?:2,
			'order_id'=>$order_id,
			'mid'=>$mid,
			'msg'=>$msg?:'异常报备',
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		$this->pdo_insert('baobei_log',$data);
		$logid=pdo_insertid();
		return $logid;
	}
	public function change_fee($order_id){
		$this->get_order($order_id);
		$setting=$this->plugin_setting('setting');
		if($setting['fee_price']){
			$fee=$setting['fee_price'];
		}elseif($setting['fee_rate']){
			$fee=$setting['fee_rate']*$this->order['price']/100;
		}
		$mid=pdo_getcolumn('jy_cxjxiche_worker',array('id'=>$this->order['worker_id']),'mid');
		model('core/member')->cut_credit2($mid,$fee,'师傅扣手续费','jy_cxjxiche');
		return true;
	}
}